package com.musiklegal;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class Songby extends ListActivity
{
	
	static String []list;
	static String [] daftar;
	static String method;
	 static boolean era;
	Handler h;
	ProgressDialog dialog;
	
	protected Dialog onCreateDialog(int id) {
        switch (id) {
              case 0: {
                    dialog = new ProgressDialog(this);
                    dialog.setMessage("Loading...");
                    dialog.setIndeterminate(true);
                    dialog.setCancelable(true);
                    return dialog;                   
              }
        }
        return null;
  }
	
	public void onCreate(Bundle savedInstanceState) 
	{
	  super.onCreate(savedInstanceState);

	  setListAdapter(new ArrayAdapter<String>(this, R.layout.abc, list));

	  ListView lv = getListView();
	  lv.setTextFilterEnabled(true);
	  h = new Handler();
	  lv.setOnItemClickListener(new OnItemClickListener()
	  {

		public void onItemClick(AdapterView<?> arg0, View arg1, final int pos,
				long arg3) {
			showDialog(0);
			try
			{
				new Thread(new Runnable(){
					
					public void run() {
						h.post(new Runnable(){

							public void run() {
						try
						{

						OnlineC.catl.clear();
						HttpClient mlist = new DefaultHttpClient(); 
						HttpGet get;
						
						if(!era)
						{
							CatalogueList.addres=new Address(("http://secure.musiklegal.com/?action=catalogue&method=get&api_key="+Browseby.apikey+method+list[pos]+"&limit=25").replace(" ", "+"),1,"");
							 get = new HttpGet(("http://secure.musiklegal.com/?action=catalogue&method=get&api_key="+Browseby.apikey+method+list[pos]+"&limit=25&offset=1").replace(" ", "+"));
						}
						else
						{
							CatalogueList.addres=new Address(("http://secure.musiklegal.com/?action=catalogue&method=get&api_key="+Browseby.apikey+method+daftar[pos]+"&limit=25").replace(" ", "+"),1,"");
							 get = new HttpGet(("http://secure.musiklegal.com/?action=catalogue&method=get&api_key="+Browseby.apikey+method+daftar[pos]+"&limit=25&offset=1").replace(" ", "+"));
						}
						
						HttpResponse mresp = mlist.execute(get);
						
						
						HttpEntity merclist = mresp.getEntity();
						SAXParserFactory spf = SAXParserFactory.newInstance();
						SAXParser sp = spf.newSAXParser();
						XMLReader xr = sp.getXMLReader();
						
						CatalogueParser cp = new CatalogueParser();
		    			xr.setContentHandler(cp);
		    			xr.parse(retrieveInputStream(merclist));
		    			OnlineC.catl.addAll(cp.catl);
		    			CatalogueList.merchant = OnlineC.merch.get(Browseby.pos);
		    			CatalogueList.thismerchant =OnlineC.merch.get(Browseby.pos);
		    			OnlineC.catl.addAll(cp.catl);
		    			Intent i = new Intent(getApplicationContext(), CatalogueList.class);
		    			TrackInfo.fromFriend=false;
		    			TrackInfo.fromCatalogue=true;
		    			
								// TODO Auto-generated method stub
								startActivity(i);
							removeDialog(0);
		    			
						}
						catch(IOException e)
						{
							e.printStackTrace();
						} 
						catch (SAXException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						} 
						catch (ParserConfigurationException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
		    			}});
					}}).start();
				
			}
			catch (Exception e) {
					 
		        }
			
			
			
			}
	    
	  });
	}
	private InputSource retrieveInputStream(HttpEntity httpEntity) {
        InputSource insrc = null;
        try {
              insrc = new InputSource(httpEntity.getContent());
        } catch (Exception e) {
        }
        return insrc;
  }	
	public void onResume()
	{
		super.onResume();
		if(!MusikLegal.internet(getApplicationContext()))
		{
			finish();
		}
		OnlineC.catl.clear();
		
	}
	
	public void onDestroy()
	{
		super.onDestroy();
		era = false;
	}
}
